package acoes;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.DaoDisciplinaXML;
import dao.interfaces.DaoDisciplina;

import modelo.Disciplina;

import acoes.interfaces.Acao;

public class AcaoAlterarDisciplina extends Acao {
	public AcaoAlterarDisciplina() {
		super("alterarDisciplina");
	}

	@Override
	public void tratarRequisicao(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idDisciplina = request.getParameter("idDisciplina");
		String codigoDisciplina = request.getParameter("codigoDisciplina");
		String nomeDisciplina = request.getParameter("nomeDisciplina");
		
		ArrayList<Disciplina> disciplinas = (ArrayList<Disciplina>)request.getSession().getAttribute("disciplinas");
		Disciplina disciplina = disciplinas.get(Integer.parseInt(idDisciplina));
		
		disciplina.setCodigo(codigoDisciplina);
		disciplina.setNome(nomeDisciplina);
		
		DaoDisciplina daoDisciplina = DaoDisciplinaXML.getInstance();
		daoDisciplina.gravarListaDisciplinas(disciplinas);
		
		ServletContext servletContext = request.getServletContext();
		RequestDispatcher dispatcher = servletContext.getRequestDispatcher("/WEB-INF/jsp/main.jsp");
		dispatcher.forward(request,response);
	}

	@Override
	public Acao clone() {
		return new AcaoAlterarDisciplina();
	}
}
